<?php


namespace app\controller\admin;


use app\services\AdminService;
use app\validates\AdminValidate;

class LoginController {
    public function index() {
        return view('/admin/login');
    }

    public function loginChecked() {
        // 接收参数
        $username = trim(request()->param('username', ''));
        $password = trim(request()->param('password', ''));
        // 参数验证
        validate(AdminValidate::class)->scene('login')->check([
            'username' => $username,
            'password' => $password,
        ]);
        // 逻辑处理
        $res = AdminService::loginChecked($username, $password);
        session('adminId', $res);
        session('username', $username);
        return successJson($res);
    }

    public function logout() {
        session('adminId', null);
        session('username', null);
        return redirect('/admin/index');
    }
}